package com.czxy.service;

import com.czxy.dao.CategoryMapper;
import com.czxy.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Liuyunqin 17674029449@163.com
 * @date 2018/12/19 15:18
 */
@Service
@Transactional
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有的分类
     * @return
     */
    public List<Category> findAllCategory(){
        //查出第一层
        Example example = new Example(Category.class);
        example.createCriteria().andEqualTo("parentId",0);
        List<Category> categories = categoryMapper.selectByExample(example);

        //第二层
        for (Category category2 : categories) {
            List<Category> categories1 = categoryMapper.findAllCategory(category2.getId());
            category2.setChildren(categories1);
            //第三层
            for (Category category3 : categories1) {
                List<Category> allCategory = categoryMapper.findAllCategory(category3.getId());
                category3.setChildren(allCategory);
            }
        }
        return categories;
    }

    /**
     * 查询所有22优化
     * @return
     */
    public List<Category> findAll(){
        return categoryMapper.selectAll();
    }
}
